using UnityEngine;

public class StrollController : MonoBehaviour
{
	[Header("Required Components")]
	public Rigidbody2D rb;

	[Header("Required Settings")]
	public Vector2 radius;

	public Vector2 offset;

	public float speed;

	public float minInterval;

	public float maxInterval;

	public Vector2 minBorder;

	public Vector2 maxBorder;

	[Header("Optional Settings")]
	public GameObject followTarget;

	public Animator animator;

	public string animatorParameter;

	public bool autoFlip;

	[Header("Runtime")]
	public Vector2 velocity;

	public Vector3 defaultScale;

	private Vector2 destination;

	public bool pause;

	private void OnEnable()
	{
		SetDestination();
		defaultScale = base.transform.localScale;
	}

	private void OnDisable()
	{
		CancelInvoke("SetDestination");
	}

	private void FixedUpdate()
	{
		velocity = Vector2.zero;
		if (!pause && Vector2.Distance(destination, base.transform.position) > 0.1f)
		{
			velocity = (destination - (Vector2)base.transform.position).normalized * speed * 50f * Time.fixedDeltaTime;
			velocity.y *= 0.7f;
			if (autoFlip)
			{
				if (velocity.x < 0f)
				{
					base.transform.localScale = new Vector3(0f - defaultScale.x, defaultScale.y, defaultScale.y);
				}
				else
				{
					base.transform.localScale = new Vector3(defaultScale.x, defaultScale.y, defaultScale.y);
				}
			}
		}
		rb.velocity = velocity;
		if ((bool)animator && !string.IsNullOrEmpty(animatorParameter))
		{
			animator.SetFloat(animatorParameter, velocity.magnitude);
		}
	}

	public void SetDestination()
	{
		Invoke("SetDestination", Random.Range(minInterval, maxInterval));
		if ((bool)followTarget)
		{
			destination.x = followTarget.transform.position.x + Random.Range(0f - radius.x, radius.x) + offset.x;
			destination.y = followTarget.transform.position.y + Random.Range(0f - radius.y, radius.y) + offset.y;
			if (minBorder.x != 0f)
			{
				destination.x = Mathf.Clamp(destination.x, minBorder.x, maxBorder.x);
			}
			if (minBorder.y != 0f)
			{
				destination.y = Mathf.Clamp(destination.y, minBorder.y, maxBorder.y);
			}
		}
	}

	private void OnDrawGizmosSelected()
	{
		Gizmos.color = Color.blue;
		Gizmos.DrawLine(base.transform.position, destination);
	}
}
